<template>
  <div class="inner-container">
    <el-form ref="queryFormRef" :model="queryParams" :inline="true">
      <el-form-item label="搜索评论" prop="keyword">
        <el-input clearable v-model="queryParams.keyword" placeholder="请输入搜索关键词"/>
      </el-form-item>
      <el-form-item label="审核状态" prop="checkState">
        <el-select clearable v-model="queryParams.checkState" placeholder="请选择审核状态">
          <el-option label="未审核" :value="0"/>
          <el-option label="通过" :value="1"/>
          <el-option label="驳回" :value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="评论时间" prop="dateRange">
        <el-date-picker
            v-model="queryParams.dateRange"
            type="daterange"
            range-separator="~"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" v-loading="loading">
      <el-table-column type="expand">
        <template #default="scope">
          <ul>
            <li><b>IP：</b>{{ scope.row.ip }}</li>
            <li><b>评论内容：</b>{{ scope.row.content }}</li>
          </ul>
        </template>
      </el-table-column>
      <el-table-column label="#" width="50">
        <template #default="scope">
          {{ (queryParams.pageNum - 1) * (queryParams.pageSize) + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="authorId" label="评论人ID" align="center"/>
      <el-table-column prop="authorName" label="评论人" align="center"/>
      <el-table-column prop="resourceType" label="资源类型" align="center"/>
      <el-table-column prop="resourceBrief" label="资源简介" align="center" show-overflow-tooltip/>
      <el-table-column prop="content" label="评论内容" show-overflow-tooltip/>
      <el-table-column prop="floor" label="楼层" align="center"/>
      <el-table-column prop="upCount" label="顶数量" align="center"/>
      <el-table-column prop="downCount" label="踩数量" align="center"/>
      <el-table-column prop="checkState" label="审核状态" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.checkState === 0" type="info">未审核</el-tag>
          <el-tag v-else-if="scope.row.checkState === 1" type="success">通过</el-tag>
          <el-tag v-else-if="scope.row.checkState === 2" type="danger">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isDisplay" label="显示状态" align="center">
        <template #default="scope">
          <el-switch
              v-model="scope.row.isDisplay"
              :active-value="1"
              :inactive-value="0"
              @change="handleDisplay(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="gmtCreated" label="创建时间" align="center" width="165"/>
      <el-table-column label="操作" fixed="right" align="center" width="150">
        <template #default="scope">
          <el-link type="primary" :icon="Checked" @click="showDialog(scope.row)">审核</el-link>
        </template>
      </el-table-column>
    </el-table>

    <br>
    <el-pagination
        background
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />

    <el-dialog v-model="dialogVisible" :title="dialogTitle" :before-close="closeDialog" append-to-body>
      <el-form ref="dialogFormRef" :model="formData" :rules="formRules" label-width="80px">
        <el-form-item label="评论编号" prop="commentId">
          <el-input v-model="formData.commentId" disabled/>
        </el-form-item>
        <el-form-item label="评论内容" prop="content">
          <el-input v-model="formData.content" type="textarea" :rows="5" disabled/>
        </el-form-item>
        <el-form-item label="审核状态" prop="checkState">
          <el-radio-group v-model="formData.checkState">
            <el-radio :label="0">未审核</el-radio>
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入审核备注" type="textarea" :rows="5"/>
        </el-form-item>
      </el-form>
      <template #footer class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import request from '@/utils/request'
import {Checked} from '@element-plus/icons-vue'
import {ElMessage} from "element-plus"

// --------------------------------------------------
const loading = ref(false)
const queryFormRef = ref()
const queryParams = ref({pageNum: 1, pageSize: 10})
const total = ref(0)
const tableData = ref([])

const listData = () => {
  loading.value = true
  request.get('/content/comment/list', {
    params: queryParams.value
  }).then(res => {
    total.value = res.data.total;
    tableData.value = res.data.list;
    loading.value = false
  })
}

const handleQuery = () => {
  queryParams.value.pageNum = 1
  listData()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  listData()
}

const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  listData()
}

// --------------------------------------------------
const dialogTitle = ref('审核')
const dialogVisible = ref(false)
const dialogFormRef = ref()
const formData = ref({})
const formRules = ref({
  checkState: [
    {required: true, message: '请选择审核状态'}
  ],
  remark: [
    {required: true, message: '请填写审核备注'}
  ]
})

const showDialog = (row) => {
  formData.value.commentId = row.id
  formData.value.content = row.content
  formData.value.checkState = row.checkState
  dialogVisible.value = true
}

const closeDialog = () => {
  formData.value = {
    id: undefined,
    checkState: undefined,
    remark: undefined,
    commentId: undefined,
    content: undefined
  }
  dialogFormRef.value.resetFields()
  dialogVisible.value = false
}

const handleSubmit = () => {
  dialogFormRef.value.validate(valid => {
    if (!valid) {
      ElMessage.error('请正确填写参数')
      return false
    }
    console.log(formData.value)
    request.post(`/content/comment/audit`, formData.value)
        .then(res => {
          ElMessage.success('保存成功')
          closeDialog()
          listData()
        })
  })
}

// --------------------------------------------------
onMounted(() => {
  listData()
})
</script>

<style scoped>

</style>
